package com.ellricky.newtv

import android.content.pm.ActivityInfo
import android.view.View
import com.andr.common.ui.activity.BaseActivity
import com.andr.common.ui.mvp.BasePresenter
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.newtv.ClassInfo
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEntityCallback
import com.ellricky.newtv.bean.TvCourseDetail
import com.ellricky.newtv.const.TvConst
import com.ellricky.newtv.utils.CacheToken
import kotlinx.android.synthetic.main.activity_new_tv_class_manage.tv_back
import kotlinx.android.synthetic.main.activity_new_tv_class_manage.tv_title
import kotlinx.android.synthetic.main.activity_new_tv_preview.*

class NewTvPreviewActivity : BaseActivity<Void, BasePresenter<Void>>() {

    private val courseId: String by lazy {
        intent.getStringExtra(TvConst.KEY_ID) ?: ""
    }

    private val classInfo: ClassInfo by lazy {
        LocalCacheUtil.readTvClassInfo()
    }

    private val classId: String by lazy {
        intent.getStringExtra(TvConst.KEY_CLASS_ID) ?: ""
    }

    override fun getContentLayout(): Int {
        return R.layout.activity_new_tv_preview
    }

    override fun setOrientation() {
        this.requestedOrientation =
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
    }

    override fun initUIView() {
        CacheToken.verifyToken()
        tv_back.setOnClickListener(this)
        loadData()
    }

    private fun loadData() {
        val url = EAppUtil.getTvApiHost()
        val paramMap = mutableMapOf(
            "action" to "wareDetail",
            "ware_id" to courseId,
            "eid" to LocalCacheUtil.getTvUid()
        )
        if (classId.isEmpty() && classInfo != null) {
            paramMap["class_id"] = classInfo.id
        } else {
            paramMap["class_id"] = classId
        }
        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEntityCallback<TvCourseDetail>(this, false) {
                override fun onSuccess(file: TvCourseDetail, var2: String?) {
                    tv_title.text = file.course_type_name
                    tv_second_title.text = file.ware_name
                    loadVideoFragment(file)
                    loadPPtFragment(file)
                    loadPdfFragment(file)
                }
            }, this)
    }

    private fun loadVideoFragment(file: TvCourseDetail) {
        val fragment = NewTvVideoPreviewFragment.newInstance(arrayListOf(file.ware_vedio))
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.container_2, fragment)
            .commitAllowingStateLoss()
    }

    private fun loadPPtFragment(file: TvCourseDetail) {
        if (file.ware_ppt.isNullOrEmpty()) {
            return
        }
        val fragment = NewTvImagePreviewFragment.newInstance(file.ware_ppt)
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.container_1, fragment)
            .commitAllowingStateLoss()
    }

    private fun loadPdfFragment(file: TvCourseDetail) {
        if (file.ware_pdf.isNullOrEmpty()) {
            return
        }
        val fragment = NewTvImagePreviewFragment.newInstance(file.ware_pdf)
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.container_3, fragment)
            .commitAllowingStateLoss()
    }

    override fun onClickView(v: View) {
        super.onClickView(v)
        when (v.id) {
            R.id.tv_back -> finish()
        }
    }
}